package com.jingan.nursinghome.regulatory.select.timu;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jingan.nursinghome.regulatory.R;
import com.jingan.nursinghome.regulatory.data.MicrometerTable;
import com.jingan.nursinghome.regulatory.http.Http;
import com.jingan.nursinghome.regulatory.model.Images;
import com.jingan.nursinghome.regulatory.model.Qfinfo;
import com.jingan.nursinghome.regulatory.select.KHttp;
import com.jingan.nursinghome.regulatory.view.MyActionBar;

import java.util.ArrayList;
import java.util.List;

import cn.android.support.v7.lib.sin.crown.base.BaseActivity;
import cn.android.support.v7.lib.sin.crown.base.BaseDialog;
import cn.android.support.v7.lib.sin.crown.base.BaseListenner;
import cn.android.support.v7.lib.sin.crown.kotlin.https.Bitmaps;
import cn.android.support.v7.lib.sin.crown.utils.ProportionUtils;
import kotlin.Unit;
import kotlin.jvm.functions.Function1;

/**
 * 评分结果页
 * Created by 彭治铭 on 2018/4/1.
 */

public class MicrometerTableResultDialog extends BaseDialog implements View.OnClickListener {
    private static MicrometerTableResultDialog resultDialog;

    static String iid;//巡检信息id
    static BaseDialog mBaseDialog;

    public static MicrometerTableResultDialog getInstance(BaseActivity baseActivity, String id, BaseDialog baseDialog) {
        mBaseDialog = baseDialog;
        iid = id;
        if (isFinishing(resultDialog)) {
            resultDialog = new MicrometerTableResultDialog(baseActivity);
        }
        return resultDialog;
    }

    public MicrometerTableResultDialog(BaseActivity baseActivity) {
        init(baseActivity, R.layout.dialog_business_results, false, false);
        initUI();
        window.setWindowAnimations(cn.android.support.v7.lib.sin.crown.R.style.Crown_Window_right);
    }

    MyActionBar myActionBar;
    Button btn_back, btn_next;

    TextView txt_signature;
    TextView txt_img1, txt_img2, txt_img3, txt_img4, txt_img5;

    TextView txt_hdfs2, txt_hdfs4, txt_jigou, txt_time, txt_goutou, txt_beizhu2;

    protected void initUI() {
        myActionBar = (MyActionBar) findViewById(R.id.MyActionBar);
        myActionBar.setLeftBimapAndTitle(null, "评分结果");
        myActionBar.setBaseListennerLeft(new BaseListenner() {
            @Override
            public void onResult(Object result) {
                dismiss();
            }
        });
        btn_back = (Button) findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        btn_next = (Button) findViewById(R.id.btn_next);
        btn_next.setOnClickListener(this);
        txt_time = (TextView) findViewById(R.id.txt_time);
        txt_signature = (TextView) findViewById(R.id.txt_signature);

        txt_img1 = (TextView) findViewById(R.id.txt_img1);
        txt_img2 = (TextView) findViewById(R.id.txt_img2);
        txt_img3 = (TextView) findViewById(R.id.txt_img3);
        txt_img4 = (TextView) findViewById(R.id.txt_img4);
        txt_img5 = (TextView) findViewById(R.id.txt_img5);

        txt_hdfs2 = (TextView) findViewById(R.id.txt_hdfs2);
        txt_hdfs4 = (TextView) findViewById(R.id.txt_hdfs4);

        txt_jigou = (TextView) findViewById(R.id.txt_jigou);
        txt_goutou = (TextView) findViewById(R.id.txt_goutou);
        txt_beizhu2 = (TextView) findViewById(R.id.txt_beizhu2);
    }



    @Override
    protected void listener() {
        KHttp.INSTANCE.xiangqin(baseActivity, iid, new BaseListenner<Qfinfo>() {
            @Override
            public void onResult(final Qfinfo qfinfo) {

                txt_hdfs2.setText(Float.valueOf(qfinfo.getTmdfzh()).intValue() + "分" + "/" + qfinfo.getTotal() + "分");//得分/总分
                txt_hdfs4.setText(qfinfo.getScoring_rate() + "%");//得分率
                txt_jigou.setText("巡检机构：" + qfinfo.getOname());//养老机构
                txt_time.setText("巡检时间：" + qfinfo.getDate());//巡检日期
                txt_goutou.setText("主要沟通人员：" + qfinfo.getLname());//主要沟通人员
                txt_beizhu2.setEnabled(false);

                ( (TextView) findViewById(R.id.txt_xunjian)).setText("巡检表类型："+qfinfo.getType());
                txt_beizhu2.setText("这个人很懒，什么都没有留下。。。");//备注
                txt_signature.setText("暂无签名图片");
                Log.e("test","签名图片:\t"+KHttp.INSTANCE.getQianming() + qfinfo.getSignature());
                new Bitmaps(KHttp.INSTANCE.getQianming() + qfinfo.getSignature(), baseActivity).Get(new Function1<Bitmap, Unit>() {
                    @Override
                    public Unit invoke(Bitmap result) {
                        float sp = (float) result.getHeight() / (float) result.getWidth();
                        Bitmap bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(result, txt_signature.getWidth(), sp);
                        txt_signature.getLayoutParams().width = bitmap.getWidth();
                        txt_signature.getLayoutParams().height = bitmap.getHeight();
                        txt_signature.requestLayout();
                        txt_signature.setBackground(new BitmapDrawable(bitmap));
                        txt_signature.setText("");
                        return null;
                    }
                });
                txt_goutou.setText("主要沟通人员：" + qfinfo.getLname());//主要沟通人员
                txt_beizhu2.setText(qfinfo.getRemark());//备注
                txt_goutou.setText("主要沟通人员：" + qfinfo.getLname());//主要沟通人员


                lilist.clear();
                lilist.add(findViewById(R.id.li1));
                lilist.add(findViewById(R.id.li2));
                lilist.add(findViewById(R.id.li3));
                lilist.add(findViewById(R.id.li4));
                lilist.add(findViewById(R.id.li5));
                for (int i = 0; i < lilist.size(); i++) {
                    lilist.get(i).setVisibility(View.GONE);
                }

                txtList.clear();
                txtList.add(txt_img1);
                txtList.add(txt_img2);
                txtList.add(txt_img3);
                txtList.add(txt_img4);
                txtList.add(txt_img5);
                KHttp.INSTANCE.qfinfoImg(baseActivity, iid, new BaseListenner<ArrayList<Images>>() {
                    @Override
                    public void onResult(ArrayList<Images> result) {
                        for (int i = 0; i < result.size(); i++) {
                            lilist.get(i).setVisibility(View.VISIBLE);
                            upImg(txtList.get(i), KHttp.INSTANCE.getImg() + result.get(i).getPolling_img());
                        }
                    }
                });

            }
        });
    }

    private List<TextView> txtList = new ArrayList<>();
    private List<View> lilist = new ArrayList<>();

    private void upImg(final TextView txt_signature, String url) {
        new Bitmaps(url, baseActivity).Get(new Function1<Bitmap, Unit>() {
            @Override
            public Unit invoke(Bitmap result) {
                float sp = (float) result.getHeight() / (float) result.getWidth();
                Bitmap bitmap = ProportionUtils.getInstance().GeometricCompressionBitmap(result, txt_signature.getWidth(), sp);
                txt_signature.getLayoutParams().width = bitmap.getWidth();
                txt_signature.getLayoutParams().height = bitmap.getHeight();
                txt_signature.requestLayout();
                txt_signature.setBackground(new BitmapDrawable(bitmap));
                txt_signature.setText("");
                return null;
            }
        });
    }

    @Override
    protected void recycleView() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_back://上一项
                dismiss();
                break;
            case R.id.btn_next://主页
                mBaseDialog.dismiss();
                dismiss();
                break;
        }
    }
}
